/*
 * SWAB.c
 *
 *  Created on: Jan 20, 2014
 *      Author: adijoshi
 */

#include "includes.h"

/*
 * Function: func_SWAB
 *
 * Inputs:	 Address_of_destination
 * Outputs:  (None)
 * Condition  Codes:
 * 			Z : set  if  low-order  byte  of  result  =  0;  cleared  otherwise
 * 			N : set  if  high-order  bit  of  low-order  byte  (bit  7)  of  result is  set:  cleared  otherwise
 * 			C : cleared
 * 			V : cleared
 *
 Rotates  all  bits  of  the  destination  left  one  place.
 Bit  15  is  loaded into  the  C-bit  of  the  status  word  and
 the  previous  contents  of  the C-bit  are loaded  into  bit  0  of  the  destination.

 */

void func_SWAB(unsigned short destination_address)
{
	unsigned short data,higher_byte,lower_byte;
	unsigned short result = 0;

	data = read_from_memory(destination_address,DATA);
	higher_byte = (data & 0xFF00);
	lower_byte = (data & 0x00FF);

	result = (lower_byte << 8) | (higher_byte >> 8);
	write_in_memory(destination_address,result,DATA);

	update_Z_flag(higher_byte);				// passing higher byte (before swap)
	update_N_flag(SWAB,result);
	update_C_flag(SWAB,0,0,result,0);
	update_V_flag(SWAB,0,0,result);
}
